local skynet = require "skynet"
local pb = require "protobuf"

local protobuf_dir = skynet.getenv "protobuf_dir"
assert(protobuf_dir,"config not set key protobuf_dir")

local proto_manager = {}

local package_name = "net.messgae."

local proto_files = {
    "MessageHeader.pb",
    "MessageId.pb",
    "ErrorCode.pb",
    "Login.pb",
    "Match.pb",
    "Frame.pb",
    "Game.pb",
    "Race.pb",
}

local function register_all_pbs(dir)
    for _,v in ipairs(proto_files) do
        pb.register_file(dir .. "/" .. v)
    end
end

register_all_pbs(protobuf_dir)

function proto_manager.message_id(id)
    return proto_manager.enum_id("MessageId",id)
end

function proto_manager.errcode(code)
    return proto_manager.enum_id("ErrorCode",code)
end

function proto_manager.enum_id(enum_type, enum_name)
    return pb.enum_id(package_name .. enum_type,enum_name)
end

function proto_manager.encode(message, t , func , ...)
    return pb.encode(package_name .. message, t, func , ...)
end

function proto_manager.decode(typename, buffer, length)
    return pb.decode(package_name .. typename,buffer,length)
end

proto_manager.ErrorCode_Successed = proto_manager.errcode("ErrorCode_Successed")

return proto_manager